<?php
class Store_model extends CI_Model {
    public function __construct()
    {
        $this->tabName = "okdkyao.dk_app_yaodian";
    }

    /*
        通过城市id获取药店列表
        params
            city_id 城市id
    */
    public function getStoreListByCityId($city_id = 0)
    {
        $areaId = $this->getAreaIdByCityId($city_id);
        if(empty($areaId)){
            return false;
        }

        $storeId = $this->getStoreIdByAreaId($areaId);

        if(empty($storeId)){
            return false;
        }

        $storeInfo = $this->getStoreInfoByStoreId($storeId,"ID,Caption");

        return $storeInfo;
    }

    /*
        通过城市id获取区域id
        params
            city_id 城市id
    */
    public function getAreaIdByCityId($city_id = 0)
    {
        $this->load->model("Common_model", "CommonModel");
        $CommonModel = $this->CommonModel;

        return $CommonModel->getAreaIdByCityId($city_id);
    }

    /*
        通过区域id获取店铺id
        params
            area_id[] 区域id数组
    */
    public function getStoreIdByAreaId($area_id = array())
    {
        $tabName = "okdkyao.dk_app_yaodian";

        $StoreInfo = $this->db
            ->select("ID")
            ->where_in("AID", $area_id)
            ->get($tabName)
            ->result_array();

        $res = array();

        foreach ($StoreInfo as $key => $value) {
            $res[] = intval($value['ID']);
        }

        return $res;
    }

    /*
        通过药店id获取药店信息
        params
            store_id[] 药店id数组
            field 查询字段       
    */
    public function getStoreInfoByStoreId($store_id = array(), $field = "*")
    {
        $tabName = "okdkyao.dk_app_yaodian";

        $StoreInfo = $this->db
            ->select($field)
            ->where_in("ID", $store_id)
            ->get($tabName)
            ->result_array();

        $res = array();

        foreach ($StoreInfo as $key => $value) {
            $res[$value['ID']] = $value;
        }

        return $res;
    }
}